package action;

import global.Constants;
import global.beanUtils.BeanUtils;
import global.security.SessionUtils;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import service.AlbumService;
import util.MessageUtils;
import util.ServletHelp;
import util.StringUtils;
import domain.Album;

/**
 * 相册Action类.
 * <p>
 * 创建日期：2010-07-01<br>
 * 创建人：Xiyt<br>
 * 修改日期：<br>
 * 修改人：<br>
 * 修改内容：<br>
 * 
 * @author Xiyt
 * @version 1.0
 */
public class AlbumAction extends BaseAction {
	/**
	 * 相册Service
	 * */
	private AlbumService albumService = null;

	/**
	 * @param albumService
	 *            the albumService to set
	 */
	public void setAlbumService(AlbumService albumService) {
		this.albumService = albumService;
	}

	/**
	 * 获得笔记分类树
	 * @param ActionMapping
	 *            mapping
	 * @param ActionForm
	 *            form
	 * @param HttpServletRequest
	 *            request
	 * @param HttpServletResponse
	 *            response
	 * @return ActionForward
	 */
	public ActionForward getAlbumTree(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		// 参数
		String parentId = request.getParameter("parentId");
		// 返回结果
		JSONArray res = albumService.selectAlbumForTree(parentId);
		ServletHelp.outRequestForJson(request, response, res.toString());
		return null;
	}

	/**
	 * 保存相册
	 * @param ActionMapping
	 *            mapping
	 * @param ActionForm
	 *            form
	 * @param HttpServletRequest
	 *            request
	 * @param HttpServletResponse
	 *            response
	 * @return ActionForward
	 */
	@SuppressWarnings("unchecked")
	public ActionForward save(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		// 参数设定
		Map map = request.getParameterMap();
		Album album = new Album();
		BeanUtils.populate(album, map);
		album.setStatus(Constants.STATUS_1);

		String albumId = request.getParameter("albumId");
		// 保存
		if (StringUtils.isNotEmpty(albumId)) {
			// 更新相册
			albumService.update(album);
		} else {
			// 添加相册
			albumService.insert(album);
		}

		// 返回结果
		JSONObject res = new JSONObject();
		res.put("success", true);
		ServletHelp.outRequestForJson(request, response, res.toString());
		return null;
	}
	
	/**
	 * 删除相册
	 * @param ActionMapping
	 *            mapping
	 * @param ActionForm
	 *            form
	 * @param HttpServletRequest
	 *            request
	 * @param HttpServletResponse
	 *            response
	 * @return ActionForward
	 */
	public ActionForward delete(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		// 上传文件的保存路径
		String uploadFilePath = ServletHelp.getRealPath(request,
				MessageUtils.setParamMessage(Constants.UPLOAD_PICTURE_PATH, new String[]{SessionUtils.getCurrentUserId()}));
		// 获得相册ID
		String albumId = request.getParameter("albumId");
		if (StringUtils.isNotEmpty(albumId)) {
			// 删除相册
			albumService.delete(Integer.parseInt(albumId), uploadFilePath);
		}
		// 返回结果
		JSONObject res = new JSONObject();
		res.put("success", true);
		ServletHelp.outRequestForJson(request, response, res.toString());
		return null;
	}
}
